package pannelli.sub;

import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;

import database.DB;

import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.swing.*;
import javax.swing.border.BevelBorder;

import oggetti.Cliente;
import oggetti.Riparazione;
import oggetti.Utente;
import org.freixas.jcalendar.DateEvent;
import org.freixas.jcalendar.DateListener;
import org.freixas.jcalendar.JCalendar;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class SubPannelloRiparazione extends javax.swing.JPanel {

    private JTextField preventivo;
    private JTextField imei;
    private JTextField id;
    private JTextField noteRiparazione;
    private JComboBox stato;
    private JTextField difetto;
//    private JTextField utente;
    private JTextField dataConsegna;
    private JTextField idCliente;
    private JCheckBox accettato;
    private static String[] stati = {"Visionato", "Non Visionato", "In Lavorazione", "In Attesa di materiale", "Reso non riparato", "Riparato"};
    private static String[] listaUtenti;//={"utente 1", "utente 2", "utente 3"};
    private JComboBox utenti;
    private JPopupMenu popup;
    private JCalendar calendario;
    
    public SubPannelloRiparazione() {
        super();
        initGUI();
        initPopup();
    }
    private void initPopup(){
       popup = new JPopupMenu();
       calendario = new JCalendar();
       calendario.setDate(new GregorianCalendar().getTime());
       popup.add(calendario);
       dataConsegna.setEditable(false);
       calendario.addDateListener(new DateListener() {
            @Override
            public void dateChanged(DateEvent de) {
                Calendar c = de.getSelectedDate();
                if (c != null) {
                    String data = c.get(Calendar.DAY_OF_MONTH) + "-" + (c.get(Calendar.MONTH) + 1) + "-" + c.get(Calendar.YEAR);
                    dataConsegna.setText(data);
                    popup.setVisible(false);
                }
            }
        });
       dataConsegna.addMouseListener(new MouseAdapter() {
          public void mouseClicked(MouseEvent e) {
             popup.show(e.getComponent(), e.getX(), e.getY());
             popup.setVisible(true);  
          }
       });
       
    }
    public void riempiIdCliente(int id) {
        this.idCliente.setText("" + id);
    }

    public void fillFields(Riparazione r) {
        this.stato.setSelectedItem(r.getStato());
        this.difetto.setText(r.getDifetto());
        this.imei.setText(r.getImei());
        this.utenti.setSelectedItem(r.getUtente());
//        this.utente.setText(r.getUtente());
        this.dataConsegna.setText(r.getDataConsegna());
        this.noteRiparazione.setText(r.getNote());
        this.id.setText("" + r.getScheda());
        this.idCliente.setText("" + r.getIdCliente());
        this.preventivo.setText("" + r.getPreventivo());
        this.accettato.setEnabled(r.getAccettato().equals("true"));

    }

    public Riparazione getRiparazioneFromFields() {
        String stato_, difetto_, imei_, utente_, dataConsegna_, note;
        int id_, idC_;
        double preventivo_ = 0;
        id_ = Integer.parseInt(id.getText());
        idC_ = Integer.parseInt(idCliente.getText());
        stato_ = (String) stato.getSelectedItem();
        utente_ = (String) utenti.getSelectedItem();
        imei_ = imei.getText().length() >= 0 ? imei.getText() : " ";
        difetto_ = difetto.getText().length() >= 0 ? difetto.getText() : " ";
        try {
            preventivo_ = preventivo.getText().length() > 0 ? Double.parseDouble(preventivo.getText().toString().replace(",", ".")) : 0.0;
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, "Attenzione! Il preventivo deve essere un valore numerico!");
            return null;
        }
        dataConsegna_ = dataConsegna.getText().length() >= 0 ? dataConsegna.getText() : " ";
        if(dataConsegna_.length()==0){
           JOptionPane.showMessageDialog(this, "Attenzione! Il campo data consegna e' obbligatorio!");
           return null;
        }
        note = noteRiparazione.getText().length() >= 0 ? noteRiparazione.getText() : " ";
        String accepted = accettato.isSelected() ? "true" : "false";
        Riparazione r = new Riparazione(id_, idC_, preventivo_, imei_, difetto_, stato_, utente_, dataConsegna_, accepted, note);
        return r;

    }

    public void riempiID() {
        String id_;
        try {
            id_ = new DB().getIDLiberiRiparazioni();
            if (id_.length() <= 0) {
                int i = new DB().queryCount("SELECT * FROM riparazione");
                id.setText("" + (i + 1));
            } else {
                id.setText(id_);
            }
        } catch (SQLException e1) {
            e1.printStackTrace();
        }

    }

    private void initGUI() {
        AnchorLayout datiClienteLayout = new AnchorLayout();
        setLayout(datiClienteLayout);
        this.setPreferredSize(new java.awt.Dimension(680, 186));
        setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
        {
            preventivo = new JTextField();
            this.add(getAccettato(), new AnchorConstraint(706, 981, 943, 738, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
            this.add(getNoteRiparazione(), new AnchorConstraint(357, 971, 631, 521, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
            this.add(preventivo, new AnchorConstraint(706, 718, 959, 577, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
            preventivo.setPreferredSize(new java.awt.Dimension(96, 47));
            preventivo.setBorder(BorderFactory.createTitledBorder("Preventivo"));
        }
        {
            imei = new JTextField();
            this.add(imei, new AnchorConstraint(706, 356, 959, 21, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
            imei.setPreferredSize(new java.awt.Dimension(228, 47));
            imei.setBorder(BorderFactory.createTitledBorder("IMEI"));
        }
        {
            id = new JTextField();
            this.add(id, new AnchorConstraint(77, 150, 325, 24, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
            id.setPreferredSize(new java.awt.Dimension(86, 46));
            id.setBorder(BorderFactory.createTitledBorder("ID Scheda"));
            id.setEditable(false);
        }
        {
            idCliente = new JTextField();
            this.add(idCliente, new AnchorConstraint(77, 284, 325, 159, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
            idCliente.setPreferredSize(new java.awt.Dimension(85, 46));
            idCliente.setBorder(BorderFactory.createTitledBorder("ID Cliente"));
            idCliente.setEditable(false);
        }
        {
            difetto = new JTextField();
            this.add(difetto, new AnchorConstraint(77, 980, 325, 302, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
            difetto.setPreferredSize(new java.awt.Dimension(461, 46));
            difetto.setBorder(BorderFactory.createTitledBorder("Difetto"));
        }
        /*{
            utente = new JTextField();
            this.add(utente, new AnchorConstraint(712, 568, 954, 365, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
            utente.setPreferredSize(new java.awt.Dimension(138, 45));
            utente.setBorder(BorderFactory.createTitledBorder("Utente"));
        }*/
        {
            dataConsegna = new JTextField();
            this.add(dataConsegna, new AnchorConstraint(373, 513, 631, 302, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
            dataConsegna.setPreferredSize(new java.awt.Dimension(144, 48));
            dataConsegna.setBorder(BorderFactory.createTitledBorder("Data consegna"));
        }
        {
            stato = new JComboBox(stati);
            this.add(stato, new AnchorConstraint(362, 284, 642, 21, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
            stato.setPreferredSize(new java.awt.Dimension(179, 52));
            stato.setBorder(BorderFactory.createTitledBorder("Stato"));
        }
        {
        	ArrayList<Utente> lista= new DB().getUtenti();
        	
            utenti = new JComboBox();
            this.add(utenti, new AnchorConstraint(712, 568, 954, 365, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
            utenti.setPreferredSize(new java.awt.Dimension(179, 52));
            utenti.setBorder(BorderFactory.createTitledBorder("Utente"));
            DefaultComboBoxModel model = new DefaultComboBoxModel();
            for (int i=0;i<lista.size();i++){
            	model.addElement(lista.get(i).getUsername());
            }
            utenti.setModel(model);
        }

    }

    private JTextField getNoteRiparazione() {
        if (noteRiparazione == null) {
            noteRiparazione = new JTextField();
            noteRiparazione.setPreferredSize(new java.awt.Dimension(306, 51));
            noteRiparazione.setBorder(BorderFactory.createTitledBorder("Note riparazione"));
        }
        return noteRiparazione;
    }

    private JCheckBox getAccettato() {
        if (accettato == null) {
            accettato = new JCheckBox();
            accettato.setText("Preventivo accettato");
            accettato.setPreferredSize(new java.awt.Dimension(165, 44));
            accettato.setEnabled(false);
        }
        return accettato;
    }
}
